package com.xunshangwang.pinhaohuo.ui.dialogs;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.xunshangwang.pinhaohuo.R;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.favorite.WechatFavorite;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import tool.library.ToastTool;


/**
 * Created by xzp on 16/3/23.
 */
public class ShareDialog extends DialogFragment{
    private RecyclerView mRecycler;
    private ShareAdapter mAdapter;
    private String mImageUrl,mTitle,mText,mUrl;
    public static final ShareDialog newDialog(String imageUrl,String title,String text,String url){
        ShareDialog shareDialog=new ShareDialog();
        Bundle bundle=new Bundle();
        bundle.putString("imageUrl",imageUrl);
        bundle.putString("title",title);
        bundle.putString("text",text);
        bundle.putString("url",url);
        shareDialog.setArguments(bundle);
        return shareDialog;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getArguments()!=null){
            Bundle bundle=getArguments();
            mImageUrl=bundle.getString("imageUrl");
            mTitle=bundle.getString("title");
            mText=bundle.getString("text");
            mUrl=bundle.getString("url");
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.dialog_share,container,false);
        mRecycler= (RecyclerView) view.findViewById(R.id.share_type_list);
        mRecycler.setLayoutManager(new GridLayoutManager(inflater.getContext(), 3));
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mAdapter=new ShareAdapter();
        mRecycler.setAdapter(mAdapter);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog=super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }

    @Override
    public void onStart() {
        super.onStart();
        getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT);
    }
    private class ShareAdapter extends RecyclerView.Adapter<ShareAdapter.ShareHolder>{
        private int[] mDrawables;
        private String[] mArrays;
        public ShareAdapter() {
            mArrays=getActivity().getResources().getStringArray(R.array.share_type_array);
            mDrawables=new int[mArrays.length];
            for(int i=0;i<mArrays.length;i++){
                mDrawables[i]=getActivity().getResources().getIdentifier("share_icon"+(i+1),"drawable",getActivity().getPackageName());
            }
        }

        @Override
        public ShareHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view=LayoutInflater.from(getContext()).inflate(R.layout.share_item,parent,false);
            return new ShareHolder(view);
        }

        @Override
        public void onBindViewHolder(ShareHolder holder, int position) {
            holder.shareIcon.setImageResource(mDrawables[position]);
            holder.shareNameText.setText(mArrays[position]);
        }

        @Override
        public int getItemCount() {
            return mArrays.length;
        }

        class ShareHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
            ImageView shareIcon;
            TextView shareNameText;
            public ShareHolder(View itemView) {
                super(itemView);
                itemView.setOnClickListener(this);
                shareIcon= (ImageView) itemView.findViewById(R.id.share_icon);
                shareNameText= (TextView) itemView.findViewById(R.id.share_name_text);
            }

            @Override
            public void onClick(View view) {
                dismiss();
                Wechat wechat= (Wechat) ShareSDK.getPlatform(Wechat.NAME);
                if(!wechat.isClientValid()){
                    ToastTool.showToast(getContext().getResources().getString(R.string.wechat_not_support_msg));
                    return;
                }
                int position=getAdapterPosition();
                Platform platform=null;
                Platform.ShareParams params=new Platform.ShareParams();
                params.setShareType(Platform.SHARE_WEBPAGE);
                params.setText(mText);
                params.setTitle(mTitle);
                params.setImageUrl(mImageUrl);
                params.setUrl(mUrl);
                switch (position){
                    case 0:
                        platform=ShareSDK.getPlatform(Wechat.NAME);
                        break;
                    case 1:
                        platform=ShareSDK.getPlatform(WechatMoments.NAME);
                        break;
                    case 2:
                        platform=ShareSDK.getPlatform(WechatFavorite.NAME);
                        break;
                    default:
                        return;
                }
                platform.share(params);
            }
        }
    }
}
